<?php

namespace App\Console\Commands;

use App\Jobs\DigArticleJobs;
use Carbon\Carbon;
use QL\QueryList;
use Illuminate\Support\Facades\Cache;
use Illuminate\Console\Command;

class DigArticle extends Command
{
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dig:article {col} {link}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'dig:article 153 https://it.ithome.com/ityejie';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $link   = $this->argument('link');
        $col    = $this->argument('col');
        $data   = $this->check($link);
        if($data){
            $index  = $this->ithome($data);
            if($index){
                $collection = collect($index);
                $multiplied = $collection->map(function ($item) use ($col) {
                    DigArticleJobs::dispatch($item,$col);
                });
                $all    = $multiplied->all();
                $this->info('dig article ok!');
            }else{
                $this->error('dig article result!');
            }
        }else{
            $this->error('dig article error!');
        }
    }

    function ithome($data){
    	$out = preg_replace("'<script[^>]*?>.*?</script>'si", '', $data);
    	$html = QueryList::html($out)->find('#list');
		//获取纯净的正文内容
        $ql = QueryList::html($html->html())->rules([
            //采集规则
            'link'  => ['.c h2 a','href'],
            'title' => ['.c h2 a','text'],
        ])->range('.fl .bl li')->query()->getData(function ($item){
            return $item;
        });
        $data   = $ql->all();
        $collection = collect($data);
        if(!$collection->isEmpty()){
            $multiplied = $collection->map(function ($item, $k) use ($collection) {
                $key    = 'dig_article_ithome'.md5($item['link']);
                $value = Cache::get($key);
                if($value){
                    $collection->pull($k);
                }else{
                    $expiresAt = Carbon::now()->addMinutes(env('DIG_CACHE'));
                    Cache::put($key, $item, $expiresAt);
                    return $item;
                }
            });
            return $multiplied->all();;
        }else{
            return false;
        }
    }
    //抓取网址
    function check($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//抓取网址
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/3.0; +http://www.baidu.com/search/spider.html)");//伪造百度蜘蛛头部
        $ip = '220.181.7.121';
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip));//伪造百度蜘蛛IP
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//
//        curl_setopt($ch, CURLOPT_HEADER, true); //抓取服务器信息
        curl_setopt($ch, CURLOPT_NOBODY, false); //抓取网页信息
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //支持301跳转
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30); //网页等待时间
        $data = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if($code['http_code'] == 200){
            return $data;
//            $parse_url  = parse_url($code['url']);
//            return      $parse_url['scheme'].'://'.$parse_url['host'];
        }else{
            return false;
        }
    }
}
